package com.xinruke.hostels.support.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xinruke.hostels.common.cache.base.factory.CacheFactory;
import com.xinruke.hostels.common.cache.base.service.interfaces.ICacheSV;
import com.xinruke.hostels.common.po.XtZidianSqlParam;
import com.xinruke.hostels.common.vo.CodeVO;
import com.xinruke.hostels.support.mapper.XtZidianSqlParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

/**
 * @author chenry
 * @ClassName XtZidianSqlParamServiceImpl
 * @date：2019/6/13
 * @version: V1.0.0
 * @description：系统字典参数SQL配置Service
 */
@Service
public class XtZidianSqlParamService{
    
    @Autowired
    private XtZidianSqlParamMapper zidianSqlParamMapper;

    
    public List<XtZidianSqlParam> queryAll() {
        return zidianSqlParamMapper.selectAll();
    }

    
    public List<XtZidianSqlParam> queryBySelective(XtZidianSqlParam sqlparam) {
        return zidianSqlParamMapper.selectBySelective(sqlparam);
    }

    
    public XtZidianSqlParam queryByCode(String code) {
        return zidianSqlParamMapper.selectByCode(code);
    }

    
    public List<CodeVO> queryBySqlLoad(String sql) {
        return zidianSqlParamMapper.selectBySql(sql);
    }

	public List<Map> queryMapBySqlLoad(String sql) {
		return zidianSqlParamMapper.selectMapBySql(sql);
	}
	
	public int addGnSqlParam(String typeCode, String paramCode, String sqlLoad) {
		XtZidianSqlParam vo = new XtZidianSqlParam();
		vo.setCanshubiaoming(typeCode);
		vo.setCanshubiaoziduan(paramCode);
		vo.setSqlload(sqlLoad);
		vo.setShanchubiaoshi(false);
		int flag = zidianSqlParamMapper.insert(vo);
		if(flag > 0) {
			this.flushCache();
		}
		return flag;
	}

	
	public int modGnSqlParamById(int id, String typeCode,
			String paramCode, String sqlLoad) {
		XtZidianSqlParam vo = new XtZidianSqlParam();
		vo.setId(id);
		vo.setCanshubiaoming(typeCode);
		vo.setCanshubiaoziduan(paramCode);
		vo.setSqlload(sqlLoad);
		int flag = zidianSqlParamMapper.updateByPrimaryKeySelective(vo);
		if(0 < flag) {
			this.flushCache();
		}
		return flag;
	}

	
	public int delGnSqlParamById(int id) {
		XtZidianSqlParam vo = new XtZidianSqlParam();
		vo.setId(id);
		vo.setShanchubiaoshi(true);
		int flag = zidianSqlParamMapper.updateByPrimaryKeySelective(vo);
		if(0 < flag) {
			this.flushCache();
		}
		return flag;
	}

	
//	public GnSqlParamVO queryById(int domainId) {
//		GnSqlParam gn = gnSqlParamMapper.selectById(domainId);
//		GnSqlParamVO vo = new GnSqlParamVO();
//		BeanUtils.copyVO(vo, gn);
//		return vo;
//	}
    
	private void flushCache() {
		//刷缓存消息增加
		ICacheSV cacheSV = CacheFactory.getCacheSV("EHTP_");
		HashMap<String, String> map = new HashMap<>();
        map.put("flushMsg", "true");
        cacheSV.pushLeft4List("fushcache", JSON.toJSONString(map));
	}
}
